package com.fr.web.core;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.core.Converter;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.html.Html;
import com.fr.base.core.html.Tag;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.util.vt.VT4FR;
import com.fr.base.core.util.vt.VT4FR.FUNC;
import com.fr.report.FloatElement;
import com.fr.report.ReportHF;
import com.fr.report.ReportPage;
import com.fr.report.ReportSettings;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.PaintUtils;
import com.fr.report.core.ReportHelper;
import com.fr.report.core.ReportUtils;
import com.fr.report.js.NameJavaScriptGroup;
import com.fr.web.Browser;
import com.fr.web.Repository;
import com.fr.web.core.chwriter.CellHtmlWriter;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HTMLConverter
  implements Converter
{
  private ReportPage reportPage;
  private Tag resTag;
  private CellHtmlWriter cellHtmlWriter;
  private ColumnRow frozenColumnRow;
  private boolean showHeaderAndFooter;
  private Repository repo;

  public HTMLConverter(ReportPage paramReportPage, CellHtmlWriter paramCellHtmlWriter, ColumnRow paramColumnRow, Repository paramRepository, Tag paramTag, boolean paramBoolean)
  {
    this.reportPage = paramReportPage;
    this.cellHtmlWriter = paramCellHtmlWriter;
    this.frozenColumnRow = paramColumnRow;
    this.repo = paramRepository;
    this.resTag = paramTag;
    this.showHeaderAndFooter = paramBoolean;
  }

  public void convert()
  {
    toTag();
  }

  private void toTag()
  {
    ReportHF localReportHF;
    Rectangle2D.Double localDouble;
    this.repo.getSessionIDInfor().clearReportPageImages();
    ReportSettings localReportSettings = this.reportPage.getReportSettings();
    int i = this.reportPage.getPageWidth();
    int j = this.reportPage.getPageHeight();
    int k = this.reportPage.getMarginTop();
    int l = this.reportPage.getMarginLeft();
    int i1 = this.reportPage.getMarginBottom();
    int i2 = this.reportPage.getMarginRight();
    int i3 = k + ((this.showHeaderAndFooter) ? ReportUtils.getHeaderHeight(localReportSettings) : 0);
    if (this.showHeaderAndFooter)
    {
      localReportHF = this.reportPage.getHeader();
      if (localReportHF != null)
      {
        localDouble = new Rectangle2D.Double(l, k, i - l - i2, ReportUtils.getHeaderHeight(localReportSettings));
        if ((localDouble.getWidth() > 0.0D) && (localDouble.getHeight() > 0.0D))
          writeReportHF(this.resTag, "HF-header", ColumnRow.validate(this.frozenColumnRow), localReportHF, localDouble, this.reportPage, this.repo);
      }
    }
    writeContent(this.resTag, this.reportPage, this.frozenColumnRow, new Rectangle2D.Double(l, i3, i - l - i2, j - k - i1 - ReportUtils.getHeaderHeight(localReportSettings) - ReportUtils.getFooterHeight(localReportSettings)), this.repo);
    if (this.showHeaderAndFooter)
    {
      localReportHF = this.reportPage.getFooter();
      if (localReportHF != null)
      {
        localDouble = new Rectangle2D.Double(l, j - i1 - ReportUtils.getFooterHeight(localReportSettings), i - l - i2, ReportUtils.getFooterHeight(localReportSettings));
        if ((localDouble.getWidth() > 0.0D) && (localDouble.getHeight() > 0.0D))
          writeReportHF(this.resTag, "HF-footer", ColumnRow.validate(this.frozenColumnRow), localReportHF, localDouble, this.reportPage, this.repo);
      }
    }
  }

  private void writeReportHF(Tag paramTag, String paramString, boolean paramBoolean, ReportHF paramReportHF, Rectangle2D paramRectangle2D, ReportPage paramReportPage, Repository paramRepository)
  {
    Tag localTag = new Tag("div").cls(paramString);
    paramTag.sub(localTag);
    localTag.css("overflow", "hidden");
    localTag.css("position", "absolute");
    if (!(paramBoolean))
    {
      localTag.css("left", (int)paramRectangle2D.getX() + "px");
      localTag.css("top", (int)paramRectangle2D.getY() + "px");
    }
    localTag.css("width", (int)paramRectangle2D.getWidth() + "px");
    localTag.css("height", (int)paramRectangle2D.getHeight() + "px");
    BufferedImage localBufferedImage = GraphHelper.createBufferedImage((int)paramRectangle2D.getWidth(), (int)paramRectangle2D.getHeight(), 2);
    Graphics2D localGraphics2D = localBufferedImage.createGraphics();
    Rectangle2D.Double localDouble = new Rectangle2D.Double(0.0D, 0.0D, paramRectangle2D.getWidth(), paramRectangle2D.getHeight());
    localGraphics2D.setPaint(Color.white);
    GraphHelper.fill(localGraphics2D, localDouble);
    paramReportHF.paint(localGraphics2D, localDouble, (int)localDouble.getWidth(), (int)localDouble.getHeight(), paramReportPage.getCurrentPageNumber(), paramReportPage.getTotalPages(), false);
    localBufferedImage.flush();
    localGraphics2D.dispose();
    paramRepository.writeTagBackground(localTag, localBufferedImage);
  }

  private void writeContent(Tag paramTag, ReportPage paramReportPage, ColumnRow paramColumnRow, Rectangle2D paramRectangle2D, Repository paramRepository)
  {
    Object localObject2;
    int i = paramReportPage.getContentWidth();
    int j = paramReportPage.getContentHeight();
    i += 2;
    j += 2;
    double d1 = 0.0D;
    double d2 = 0.0D;
    ReportSettings localReportSettings = paramReportPage.getReportSettings();
    if (localReportSettings.isHorizontalCenterOnPage())
      d1 = (paramRectangle2D.getWidth() - i) / 2.0D;
    if (localReportSettings.isVerticalCenterOnPage())
      d2 = (paramRectangle2D.getHeight() - j) / 2.0D;
    Tag localTag1 = new Tag("div");
    paramTag.sub(localTag1);
    if (paramRepository.getBrowser().shouldSetOverflowAsHiddenOnSheetContainer())
      localTag1.css("overflow", "hidden");
    localTag1.css("position", "absolute");
    if (!(ColumnRow.validate(paramColumnRow)))
    {
      localTag1.css("left", (d1 + paramRectangle2D.getX()) + "px");
      localTag1.css("top", (d2 + paramRectangle2D.getY()) + "px");
    }
    localTag1.css("width", (i + 5) + "px");
    localTag1.css("height", (j + 5) + "px");
    if (localReportSettings.getBackground() != null)
      HTMLWriterUtils.writeBackground(localTag1, localReportSettings.getBackground(), new ReportBackgroundDimension(i + 5, j + 5), false, paramRepository, false);
    HTMLWriter localHTMLWriter = createHTMLWriterCase();
    DynamicValueList localDynamicValueList1 = ReportHelper.createColumnWidthList(paramReportPage);
    DynamicValueList localDynamicValueList2 = ReportHelper.createRowHeightList(paramReportPage);
    if ((VT4FR.FROZEN_VIEW.support()) && (ColumnRow.validate(paramColumnRow)))
    {
      localTag1.cls("frozen-page");
      localObject1 = new Tag("div");
      localObject2 = new Tag("div");
      Tag localTag2 = new Tag("div");
      Tag localTag3 = new Tag("div");
      localHTMLWriter.writeFrozenContentWithCenterCornerNW(localTag1, paramReportPage, paramColumnRow, 0, this.cellHtmlWriter, paramRepository, (Tag)localObject1, (Tag)localObject2, localTag2, localTag3);
      Iterator localIterator = paramReportPage.floatIterator();
      while (true)
      {
        if (!(localIterator.hasNext()))
          return;
        FloatElement localFloatElement = (FloatElement)localIterator.next();
        int i1 = localFloatElement.getColumn();
        int i2 = localFloatElement.getRow();
        int i3 = 0;
        int i4 = 0;
        Object localObject3 = localTag1;
        if ((i1 < paramColumnRow.column) && (i2 < paramColumnRow.row))
        {
          localObject3 = localObject2;
          i3 = localDynamicValueList1.getRangeValueFromZero(i1) + localFloatElement.getLeftDistance();
          i4 = localDynamicValueList2.getRangeValueFromZero(i2) + localFloatElement.getTopDistance();
        }
        else if (i1 < paramColumnRow.column)
        {
          localObject3 = localTag3;
          i3 = localDynamicValueList1.getRangeValueFromZero(i1) + localFloatElement.getLeftDistance();
          i4 = localDynamicValueList2.getRangeValue(paramColumnRow.row, i2) + localFloatElement.getTopDistance();
        }
        else if (i2 < paramColumnRow.row)
        {
          localObject3 = localTag2;
          i3 = localDynamicValueList1.getRangeValue(paramColumnRow.column, i1) + localFloatElement.getLeftDistance();
          i4 = localDynamicValueList2.getRangeValueFromZero(i2) + localFloatElement.getTopDistance();
        }
        else
        {
          localObject3 = localObject1;
          i3 = localDynamicValueList1.getRangeValue(paramColumnRow.column, i1) + localFloatElement.getLeftDistance();
          i4 = localDynamicValueList2.getRangeValue(paramColumnRow.row, i2) + localFloatElement.getTopDistance();
        }
        writeFloatElementTag(localFloatElement, (Tag)localObject3, i3, i4);
      }
    }
    localTag1.sub(localHTMLWriter.writeJSReport(paramReportPage, 0, this.cellHtmlWriter, paramRepository));
    Object localObject1 = paramReportPage.floatIterator();
    while (((Iterator)localObject1).hasNext())
    {
      localObject2 = (FloatElement)((Iterator)localObject1).next();
      int k = localDynamicValueList1.getRangeValueFromZero(((FloatElement)localObject2).getColumn()) + ((FloatElement)localObject2).getLeftDistance();
      int l = localDynamicValueList2.getRangeValueFromZero(((FloatElement)localObject2).getRow()) + ((FloatElement)localObject2).getTopDistance();
      writeFloatElementTag((FloatElement)localObject2, localTag1, k, l);
    }
  }

  private void writeFloatElementTag(FloatElement paramFloatElement, Tag paramTag, int paramInt1, int paramInt2)
  {
    Object localObject = null;
    NameJavaScriptGroup localNameJavaScriptGroup = paramFloatElement.getNameHyperlinkGroup();
    if ((localNameJavaScriptGroup != null) && (localNameJavaScriptGroup.size() > 0))
    {
      localObject = new Tag("span");
      String str = "{}";
      try
      {
        str = HTMLWriterUtils.writeJSLinkContent(localNameJavaScriptGroup, this.repo);
      }
      catch (JSONException localJSONException)
      {
        FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      }
      ((Tag)localObject).attr("onclick", "FR.doHyperlink(event ||window.event," + str + ")");
      ((Tag)localObject).css("cursor", "pointer");
    }
    int i = paramFloatElement.getSize().width + 1;
    int j = paramFloatElement.getSize().height + 1;
    Tag localTag1 = new Tag("div");
    if (localObject == null)
      localObject = localTag1;
    else
      ((Tag)localObject).sub(localTag1);
    paramTag.sub((Html)localObject);
    localTag1.css("overflow", "hidden");
    localTag1.css("position", "absolute");
    localTag1.css("left", paramInt1 + "px");
    localTag1.css("top", paramInt2 + "px");
    localTag1.css("width", i + "px");
    localTag1.css("height", j + "px");
    Tag localTag2 = paramFloatElement.toTag(this.repo);
    this.repo.writeTagBackground(localTag2, paint4Float(paramFloatElement, i, j));
    localTag1.sub(localTag2);
  }

  protected HTMLWriter createHTMLWriterCase()
  {
    return HTMLWriter.getInstance();
  }

  private Image paint4Float(FloatElement paramFloatElement, int paramInt1, int paramInt2)
  {
    BufferedImage localBufferedImage = GraphHelper.createBufferedImage(paramInt1, paramInt2, 6);
    Graphics2D localGraphics2D = localBufferedImage.createGraphics();
    localGraphics2D.setPaint(Color.white);
    PaintUtils.paintBackground(localGraphics2D, paramFloatElement.getStyle(), paramInt1 - 1, paramInt2 - 1);
    PaintUtils.paintBorder(localGraphics2D, paramFloatElement.getStyle(), paramInt1 - 1, paramInt2 - 1);
    localBufferedImage.flush();
    localGraphics2D.dispose();
    return localBufferedImage;
  }
}